﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Entity.Validation.Core;
using WPFEntityValidation.CustomAdorners;

namespace WPFEntityValidation.Utilities
{
    public class AttachedProperties : DependencyObject
    {
        private static void RequiredFieldElementLoaded(object sender, RoutedEventArgs e)
        {
            //add the RequiredFieldAdorner
            DataTemplate dt = Application.Current.TryFindResource("RequiredFieldAdornerTemplate") as DataTemplate;
            if (dt != null &&
                sender is UIElement)
            {
                AdornerLayer al = AdornerLayer.GetAdornerLayer(sender as UIElement);
                //bool isRequiredAdorned = AdornerUtilities.UIElementIsAdornedWith<RequiredFieldAdorner>(sender as UIElement);

                if ((!((sender as UIElement) is CheckBox)))
                {
                    var entity = ((DependencyObject)sender).GetValue(ValidateEntityProperty) as IValidateEntity;
                    var fieldName = ((DependencyObject)sender).GetValue(ValidateFieldNameProperty) as String;
                    if (!entity.NotNull() || !fieldName.NotEmpty())
                        return;
                    string tooltip = entity.ValidateEntities().FirstOrDefault(x => x.ToLower().Contains(fieldName.ToLower()));
                    
                    if (tooltip.NotEmpty())
                    {
                        var sa = new RequiredFieldAdorner(sender as UIElement,
                                                          al,
                                                          dt,
                                                          tooltip);
                    }
                    else
                    {
                        AdornerUtilities.DetachAdorner<RequiredFieldAdorner>(sender as UIElement);
                    }
                }
            }

            FrameworkElement fe = (FrameworkElement)sender;
            fe.Loaded -= RequiredFieldElementLoaded;
        }

        public static readonly DependencyProperty ValidateEntityProperty =
            DependencyProperty.RegisterAttached("ValidateEntity", typeof (IValidateEntity), typeof (AttachedProperties), new PropertyMetadata(default(IValidateEntity)));

        public static void SetValidateEntity(UIElement element, IValidateEntity value)
        {
            element.SetValue(ValidateEntityProperty, value);
        }

        public static IValidateEntity GetValidateEntity(UIElement element)
        {
            return (IValidateEntity) element.GetValue(ValidateEntityProperty);
        }

        public static readonly DependencyProperty ValidateFieldNameProperty =
            DependencyProperty.RegisterAttached("ValidateFieldName", typeof (String), typeof (AttachedProperties), new PropertyMetadata(default(String), OnValidateFieldNamePropertyChangedCallback));

        private static void OnValidateFieldNamePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            {
                FrameworkElement fe = (FrameworkElement)dependencyObject;

                if (!fe.IsLoaded)
                    fe.Loaded += RequiredFieldElementLoaded;
                else
                    RequiredFieldElementLoaded(fe, null);

                if (fe is TextBox)
                {
                    ((TextBox)fe).TextChanged += (sender, args) =>
                    {
                        if (!fe.IsLoaded)
                            fe.Loaded += RequiredFieldElementLoaded;
                        else
                            RequiredFieldElementLoaded(sender, null);
                        //if (((TextBox)sender).Text.NotEmpty())
                        //{
                        //    //RemoveRequiredValidation(fe);
                        //    fe.Loaded -= RequiredFieldElementLoaded;
                        //    //remove adorner
                        //    AdornerUtilities.DetachAdorner<RequiredFieldAdorner>(dependencyObject as UIElement);
                        //}
                        //else
                        //{
                        //    if (!fe.IsLoaded)
                        //        fe.Loaded += RequiredFieldElementLoaded;
                        //    else
                        //        RequiredFieldElementLoaded(fe, null);
                        //}

                    };
                }
            }
        }

        public static void SetValidateFieldName(UIElement element, String value)
        {
            element.SetValue(ValidateFieldNameProperty, value);
        }

        public static String GetValidateFieldName(UIElement element)
        {
            return (String) element.GetValue(ValidateFieldNameProperty);
        }
    }
}